/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzilla.desk;

import com.yy.bugzilla.desk.tool.BasePane;
import com.yy.bugzillahelp.action.BugAction;
import com.yy.bugzillahelp.data.BugCommentData;
import com.yy.bugzillahelp.data.BugDetailData;
import com.yy.bugzillahelp.data.server.ServerData;
import com.yy.bugzillahelp.db.BugzillaHelpDB;
import com.yy.bugzillahelp.tool.SQLTool;
import java.sql.Connection;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 *
 * @author Alan
 */
public class BugDetailPane extends BasePane {

    public static final String BUG = "Bug";
    private ServerData serverData;
    private String bugId;
    private BugDetailData detailData;
    private VBox vBox;
    private Text titleText;
    private Label reportor = new Label("");
    private Label assignedTo = new Label("");
    private Label createTime = new Label("");
    private Label modifyTime = new Label("");
    private Label product = new Label("");
    private Label component = new Label("");
    private Label version = new Label("");
    private Label status = new Label("");
    private Label solution = new Label("");
    private Label priotity = new Label("");
    private Label severity = new Label("");

    public BugDetailPane(ServerData serverData, String bugId) {
        this.serverData = serverData;
        this.bugId = bugId;
    }

    private void setData() {
        titleText.setText(detailData.getBugId() + " - " + detailData.getShortDesc());

        reportor.setText(detailData.getReporter());
        assignedTo.setText(detailData.getAssignedTo());
        component.setText(detailData.getComponent());
        product.setText(detailData.getProduct());
        priotity.setText(detailData.getPriority());
        severity.setText(detailData.getSeverity());
        solution.setText(detailData.getResolution());
        status.setText(detailData.getStatus());
        version.setText(detailData.getVersion());
        createTime.setText(detailData.getCreateTime());
        modifyTime.setText(detailData.getModifyTime());

        for (BugCommentData bugCommentData : detailData.getDescDatas()) {
            vBox.getChildren().add(generateCommentText(bugCommentData));
        }
    }

    @Override
    protected Parent generatePane() {
        vBox = new VBox();
        vBox.setSpacing(5);
        vBox.getChildren().add(generateTitleText());
        vBox.getChildren().add(generateDetailPane());

        initDatas();
        setData();

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(vBox);
        scrollPane.setFitToWidth(true);
//        scrollPane.setFitToHeight(true);

        return scrollPane;
    }

    private Label createEmptyLabel() {
        Label label = new Label("");
//        label.setPrefWidth(100);
        return label;
    }

    private Label createDescLabel(String desc) {
        Label label = new Label(desc);
        label.setFont(Font.font(label.getFont().getName(), 14));
//        GridPane.setHalignment(label, HPos.RIGHT);有效
        return label;
    }

    private GridPane generateDetailPane() {
        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(5);
        gridPane.setPadding(new Insets(10, 10, 10, 10));
        int row = 0;
        gridPane.add(createEmptyLabel(), 0, row);
        gridPane.add(createEmptyLabel(), 1, row);
        gridPane.add(createEmptyLabel(), 2, row);
        gridPane.add(createEmptyLabel(), 3, row);
        row++;
        gridPane.add(createDescLabel("报告人："), 0, row);
        gridPane.add(reportor, 1, row);
        gridPane.add(createDescLabel("委托人："), 2, row);
        gridPane.add(assignedTo, 3, row);
        row++;
        gridPane.add(createDescLabel("创建时间："), 0, row);
        gridPane.add(createTime, 1, row);
        gridPane.add(createDescLabel("修改时间："), 2, row);
        gridPane.add(modifyTime, 3, row);
        row++;
        gridPane.add(createDescLabel("产品："), 0, row);
        gridPane.add(product, 1, row);
        gridPane.add(createDescLabel("组件："), 2, row);
        gridPane.add(component, 3, row);
        row++;
        gridPane.add(createDescLabel("版本："), 0, row);
        gridPane.add(version, 1, row);
        gridPane.add(new Label(""), 2, row);
        gridPane.add(new Label(""), 3, row);
        row++;
        gridPane.add(createDescLabel("状态："), 0, row);
        gridPane.add(status, 1, row);
        gridPane.add(createDescLabel("解决方案："), 2, row);
        gridPane.add(solution, 3, row);
        row++;
        gridPane.add(createDescLabel("优先级："), 0, row);
        gridPane.add(priotity, 1, row);
        gridPane.add(createDescLabel("紧急程度："), 2, row);
        gridPane.add(severity, 3, row);
        row++;
        gridPane.add(createEmptyLabel(), 0, row);
        gridPane.add(createEmptyLabel(), 1, row);
        gridPane.add(createEmptyLabel(), 2, row);
        gridPane.add(createEmptyLabel(), 3, row);
        row++;

        gridPane.setAlignment(Pos.CENTER);

        ColumnConstraints column1 = new ColumnConstraints();
        column1.setHalignment(HPos.RIGHT);
        column1.setPercentWidth(20);
        gridPane.getColumnConstraints().add(column1);

        ColumnConstraints column2 = new ColumnConstraints();
        column2.setHalignment(HPos.LEFT);
        column2.setPercentWidth(30);
        gridPane.getColumnConstraints().add(column2);

        ColumnConstraints column3 = new ColumnConstraints();
        column3.setHalignment(HPos.RIGHT);
        column3.setPercentWidth(20);
        gridPane.getColumnConstraints().add(column3);

        ColumnConstraints column4 = new ColumnConstraints();
        column4.setHalignment(HPos.LEFT);
        column4.setPercentWidth(30);
        gridPane.getColumnConstraints().add(column4);

//        gridPane.setGridLinesVisible(true);
        return gridPane;
    }

    private Text generateTitleText() {
        titleText = new Text();
        titleText.setFont(Font.font(titleText.getFont().getName(), 18));
        VBox.setMargin(titleText, new Insets(10, 10, 10, 10));
        return titleText;
    }

    private Parent generateCommentText(BugCommentData commentData) {
        final BorderPane borderPane = new BorderPane();
        final Label textArea = new Label(commentData.getText());
//        textArea.setTextAlignment(TextAlignment.LEFT);
        BorderPane.setAlignment(textArea, Pos.CENTER_LEFT);
//        textArea.setStyle("-fx-border-color: red;");
        textArea.setWrapText(true);//有时候可能导致文字无法显示
        final Button button = new Button("-");
        final Label time = new Label(commentData.getWho()+" "+commentData.getWhen());
        BorderPane.setAlignment(time, Pos.CENTER_LEFT);
        final Label desc = new Label(commentData.getText().replaceAll("\\s+", " "));
        BorderPane.setAlignment(desc, Pos.CENTER_LEFT);
        
        final BorderPane topPane =new BorderPane();
        topPane.setLeft(button);
        topPane.setCenter(time);
        
        borderPane.setTop(topPane);
        borderPane.setCenter(textArea);
        borderPane.setPadding(new Insets(10));

        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                if (button.getText().equals("-")) {
                    borderPane.setCenter(null);
                    button.setText("+");
                    topPane.setCenter(desc);
                    topPane.setRight(time);
                } else {
                    borderPane.setCenter(textArea);
                    button.setText("-");
                    topPane.setRight(null);
                    topPane.setCenter(time);
                }
            }
        });
        return borderPane;
    }

    private BugDetailData initDatas() {
        Connection con = BugzillaHelpDB.getConnection();
        detailData = BugAction.searchById(con, bugId, serverData.getId());
        SQLTool.closeConnection(con);
        return detailData;
    }
}
